<?php

/**
 * WebUser class.
 * 
 */
class WebUser extends CWebUser {

    private $_instance;

    /**
     * Check if user is a developer
     * @return int 
     */
    function getIsDeveloper() {
        $user = $this->instance;
        return ( $user && $user->access_role == User::ROLE_DEVELOPER );
    }

    /**
     * Check if user is an administrator
     * @return int 
     */
    function getIsAdmin() {
        $user = $this->instance;
        return ( $user && $user->access_role >= User::ROLE_ADMIN );
    }

    /**
     * Set the current user's group's last broadcast time.
     */
    public function updateLastBroadcastTime() {
        $group = Group::model()->findByPk($this->instance->group->id);
        $group->last_broadcast_time = new CDbExpression('NOW()');
        if ($group->save())
            return true;
        else
            return false;
    }

    /**
     * Get the current user's fullname.
     * @return string 
     */
    public function getFullname() {
        if ($this->instance && $fullname = $this->instance->first_name . ' ' . $this->instance->last_name)
            return $fullname;
        
        return false;
    }

    /**
     * Load the user instance model.
     * @return Object 
     */
    protected function getInstance() {
        if ($this->isGuest)
            return null;

        if ($this->_instance === null) {
            $this->_instance = User::model()->with('group')->findByPk($this->id);
        }

        return $this->_instance;
    }

}